package de.tu_darmstadt.gdi1.xcom.tests.testcases.students;

import static org.junit.Assert.assertEquals;

import java.util.HashMap;

import org.junit.Before;
import org.junit.Test;

import de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2;

/**
 * 
 * Tests if left turns are correctly saved in savegame
 * 
 * @author Lukas Schwoebel
 *
 */
public class TurnsLeftTest {
	
	private XcomTestAdapterLevel2 testAdapter;
	
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		testAdapter = new XcomTestAdapterLevel2();
	}

	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#turnsLeft()}.
	 */
	@Test
	public final void testRoundsLeftAfterLoading() {
		
		HashMap<String, Integer> turnsLeft = new HashMap<String, Integer>();

		turnsLeft.put("", 3);
		turnsLeft.put("//{gamedata}\n//activeteam:@\n//playedrounds:0\n//maxrounds:1", 1);
		turnsLeft.put("//{gamedata}\n//activeteam:@\n//playedrounds:1\n//maxrounds:3", 2);
		turnsLeft.put("//{gamedata}\n//activeteam:@\n//playedrounds:0\n//maxrounds:5", 5);
		turnsLeft.put("//{gamedata}\n//activeteam:@\n//playedrounds:2\n//maxrounds:12", 10);
		turnsLeft.put("//{gamedata}\n//activeteam:@\n//playedrounds:0\n//maxrounds:42", 42);
		turnsLeft.put("//{gamedata}\n//activeteam:@\n//playedrounds:3\n//maxrounds:3", 0);
		
		for (String level : turnsLeft.keySet()) {
			
			String saveGameString = ((level.equals("")) ? TestLevel.saveGame1Level.trim() : TestLevel.saveGame1Level + level);
			testAdapter.loadLevelFromString(saveGameString);
			assertEquals("The number of turns left was returned incorrectly by your implementation, should be" + turnsLeft.get(level) + ", was " + testAdapter.turnsLeft(), 
					turnsLeft.get(level), 
					Integer.valueOf(testAdapter.turnsLeft()));
		}
	}
	
	/**
	 * Test method for {@link de.tu_darmstadt.gdi1.xcom.tests.adapters.XcomTestAdapterLevel2#turnsLeft()}.
	 */
	@Test
	public final void testRoundsDecremented() {
		
		testAdapter.loadLevelFromString(TestLevel.saveGame1);
		// end turn
		testAdapter.endTurn();
		
		assertEquals("After ending the turn, only 1 turn should be left", 1, testAdapter.turnsLeft());
		
		// now save as savegame and check if turns are correctly inserted
		String saveGameAfterEndTurn = testAdapter.returnGamestateAsSavegame();
		String expectedSaveGameTurns = TestLevel.saveGame1Level + "//{gamedata}\n//activeteam:@\n//playedrounds:1\n//maxrounds:2";

		// remove any additional info at the end of the savegame
		// replace doors-line if there is any and remove any additional info if there is any
		saveGameAfterEndTurn = saveGameAfterEndTurn.replaceFirst("(?m)//\\{doors\\}\n", "");
		saveGameAfterEndTurn = saveGameAfterEndTurn.replaceFirst("(?s)//maxrounds:2(.*)$", "//maxrounds:2");
		
		assertEquals("The number of left turns should be saved correctly in the savegame", expectedSaveGameTurns, saveGameAfterEndTurn);
		
		testAdapter.loadLevelFromString(saveGameAfterEndTurn);
		
		// after loading the savegame, the number of turns should still be identical
		assertEquals("After loading the same savegame, the number of turns should still be the same", 1, testAdapter.turnsLeft());
		
	}
}
